package org.spring.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.tool.core.lang.Assert;

import java.util.List;
import java.util.Objects;

/**
 * 包含一个或多个 {@link PropertyValue} 对象的持有者，通常包含针对特定目标 bean 的一次更新。
 *
 * @author 李坤
 * @date 2022/4/22 16:04
 */
@Data
@AllArgsConstructor
public class MutablePropertyValues implements PropertyValues {

    private final List<PropertyValue> propertyValueList;

    @Override
    public PropertyValue[] getPropertyValues() {
        return propertyValueList.toArray(new PropertyValue[0]);
    }

    @Override
    public PropertyValue getPropertyValue(String propertyName) {
        Assert.hasText(propertyName, "propertyName 不能为空");

        for (PropertyValue propertyValue : propertyValueList) {
            if (propertyName.equals(propertyValue.getName())) {
                return propertyValue;
            }
        }
        return null;
    }

    @Override
    public boolean contains(String propertyName) {
        return Objects.nonNull(getPropertyValue(propertyName));
    }

    @Override
    public boolean isEmpty() {
        return propertyValueList.isEmpty();
    }
}
